﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Media.Devices;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Microsoft.Media.CaptureClient.Sample
{
    public sealed partial class SettingsView : UserControl
    {
        public SettingsView()
        {
            this.InitializeComponent();

            this.DataContext = Preferences.Current;
            var noawait = InitAsync();
        }

        async Task InitAsync()
        {
            switch (Preferences.Current.VideoEncodingPreferences)
            {
                case CaptureClient.VideoEncodingPreferences.MultiResolution:
                    RadioButtonMultiResolution.IsChecked = true;
                    break;
                case CaptureClient.VideoEncodingPreferences.MultiBitrate:
                    RadioButtonMultiBitrate.IsChecked = true;
                    break;
                case CaptureClient.VideoEncodingPreferences.SingleBitrate:
                    RadioButtonSingleBitrate.IsChecked = true;
                    break;
            }

            var videoDevices = await DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
            videoSources.ItemsSource = videoDevices;
            var device = videoDevices.FirstOrDefault(d => d.Id == Preferences.Current.VideoDeviceId) ?? videoDevices.FirstOrDefault();
            videoSources.SelectedItem = device;
            videoSources.SelectionChanged += videoSources_SelectionChanged;

            var audioDevices = await DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.AudioCapture);
            audioSources.ItemsSource = audioDevices;
            var audioDeviceId = MediaDevice.GetDefaultAudioCaptureId(AudioDeviceRole.Communications);
            audioSources.SelectedItem = audioDevices.FirstOrDefault(d => d.Id == Preferences.Current.AudioDeviceId) ?? audioDevices.FirstOrDefault(d => d.Id == audioDeviceId);
            audioSources.SelectionChanged += audioSources_SelectionChanged;
        }

        void audioSources_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Any())
            {
                Preferences.Current.AudioDeviceId = ((DeviceInformation)e.AddedItems.First()).Id;
            }
        }

        void videoSources_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Any())
            {
                var device = (DeviceInformation)e.AddedItems.First();
                Preferences.Current.VideoDeviceId = device.Id;
            }
        }

        private void RadioButtonMultiResolution_Checked(object sender, RoutedEventArgs e)
        {
            Preferences.Current.VideoEncodingPreferences = CaptureClient.VideoEncodingPreferences.MultiResolution;
        }

        private void RadioButtonMultiBitrate_Checked(object sender, RoutedEventArgs e)
        {
            Preferences.Current.VideoEncodingPreferences = CaptureClient.VideoEncodingPreferences.MultiBitrate;
        }

        private void RadioButtonSingleBitrate_Checked(object sender, RoutedEventArgs e)
        {
            Preferences.Current.VideoEncodingPreferences = CaptureClient.VideoEncodingPreferences.SingleBitrate;
        }
    }
}
